package com.moyuaninfo.myyserver.framework.json;

import com.moyuaninfo.myyserver.framework.enums.JsonResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author 祥霸
 * @date 2019/8/21
 */
@Data
@ApiModel("JsonResult")
public class JsonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 状态码*/
    @ApiModelProperty("状态码")
    private int code;

    /** 提示信息*/
    @ApiModelProperty("提示信息")
    private String msg;

    /** 返回的数据*/
    @ApiModelProperty("返回的数据")
    private T data;

    public void success(T data) {
        dueResult(this, JsonResultEnum.SUCCESS, data);
    }

    public void success(T data, String successMsg) {
        dueResult(this, JsonResultEnum.SUCCESS, data, successMsg);
    }

    public void error(T data) {
        dueResult(this, JsonResultEnum.FAIL, data);
    }

    public void error(T data, String errorMsg) {
        dueResult(this, JsonResultEnum.FAIL, data, errorMsg);
    }

    private void dueResult(JsonResult result, JsonResultEnum jsonResultEnum, T data) {
        result.setMsg(jsonResultEnum.getMsg());
        result.setCode(jsonResultEnum.getCode());
        result.setData(data);
    }

    private void dueResult(JsonResult result, JsonResultEnum jsonResultEnum, T data, String msg) {
        result.setMsg(msg);
        result.setCode(jsonResultEnum.getCode());
        result.setData(data);
    }
}
